﻿
@{
    Layout = "~/Views/Shared/_LayoutDialog.cshtml";
}
@model OL.FrameCore.Application.Article.Dto.GetArticleResponse
@section CSS{

}

<form class="layui-form" id="DeptForm">
    <div class="layui-tab-content page-detail">
        <div class="layui-tab-item layui-show">
            <input type="hidden" id="hidId" value="@Model.Id" />
            <div class="layui-row">
                <div class="layui-form-item">
                    <label class="layui-form-label">标题<span class="required">*</span></label>
                    <div class="layui-input-block">
                        <input type="text" id="tbTitle" name="tbTitle" lay-verify="required|maxLength200" placeholder="" autocomplete="off" class="layui-input" value="@Model.Title">
                    </div>
                </div>
            </div>
            <div class="layui-row">
                <div class="layui-form-item">
                    <label class="layui-form-label">内容<span class="required">*</span></label>
                    <div class="layui-input-block">
                        <script id="editor" type="text/plain" style="width:100%;height:500px;">
                        </script>
                    </div>
                </div>
            </div>
            <div class="layui-row">
                <div class="layui-form-item">
                    <label class="layui-form-label">标签<span class="required">*</span></label>
                    <div class="layui-input-block">
                        <input type="text" id="tbTags" name="tbTags" lay-verify="required|maxLength20" placeholder="" autocomplete="off" class="layui-input" value="@Model.Tags">
                    </div>
                </div>
            </div>
            <div class="layui-row">
                <div class="layui-form-item">
                    <label class="layui-form-label">概要<span class="required">&nbsp;</span></label>
                    <div class="layui-input-block">
                        <input type="text" id="tbSummary" name="tbSummary" lay-verify="maxLength200" placeholder="" autocomplete="off" class="layui-input" value="@Model.Summary">
                    </div>
                </div>
            </div>
            <div class="layui-row detail-submit">
                <div class="layui-layer-btn layui-layer-btn-c">
                    <a class="layui-layer-btn0" id="btnSave" lay-submit lay-filter="*">保存</a>
                    <a class="layui-layer-btn0" id="btnCanel" onclick="ArticleAddOrUpdate.cancel()">取消</a>
                </div>
            </div>
        </div>
    </div>
</form>

@section Scripts{
    <script src="~/lib/jquery/dist/jquery.js"></script>
    <script src="~/lib/ueditor1.4.3.3/ueditor.config.js"></script>
    <script src="~/lib/ueditor1.4.3.3/ueditor.all.js"></script>
    <script>
        //实例化编辑器
        var ue = UE.getEditor('editor', {
            enableAutoSave: false,
            toolbars: [[
                'fullscreen', 'source', 'preview', 'searchreplace', '|', 'undo', 'redo', '|',
                'simpleupload', 'insertimage', 'template'
            ],
            ['bold', 'italic', 'underline', 'fontborder', 'strikethrough', 'superscript', 'subscript', 'removeformat', 'formatmatch', 'autotypeset', 'blockquote', 'pasteplain', '|', 'forecolor', 'backcolor', 'insertorderedlist', 'insertunorderedlist', 'selectall', 'cleardoc', '|',
                'rowspacingtop', 'rowspacingbottom', 'lineheight', '|',
                'customstyle', 'paragraph', 'fontfamily', 'fontsize', '|',
                'directionalityltr', 'directionalityrtl', 'indent', '|',
                'justifyleft', 'justifycenter', 'justifyright', 'justifyjustify', '|', 'touppercase', 'tolowercase', '|',
                'link', 'unlink', 'anchor', '|', 'imagenone', 'imageleft', 'imageright', 'imagecenter']]
        });

        //设置内容
        ue.ready(function () {
            ue.setContent('@Html.Raw(Model.Content?.Replace("\n",""))');
        });

        var ArticleAddOrUpdate = function () {
            let inited = false;
            let data = {};

            function initData() {
                layui.use(['form', 'layer'], function () {
                    var form = layui.form;
                    form.verify({
                        maxLength20: [/^.{0,20}$/, '长度最大为20'],
                        maxLength200: [/^.{0,200}$/, '长度最大为200']
                    });

                    form.on('submit(*)', function (data) {

                        var id = $("#hidId").val();
                        var title = $("#tbTitle").val();
                        var content = ue.getContent();
                        var tags = $("#tbTags").val();
                        var summary = $("#tbSummary").val();

                        var type = id == 0 ? "添加" : "修改";

                        //开启loading
                        layer = layui.layer;
                        var index = layer.load(2);
                        if (id == 0) {//添加
                            $.ajax({
                                url: "/Article/AddArticle",
                                type: "POST",
                                data: { title: title, content: content, tags: tags, summary: summary },
                                success: function (data) {
                                    layer.close(index)
                                    if (data.state == 1) {
                                        parent.layer.msg("添加成功", { shade: 0.1, icon: 1 }, function () {
                                            //parent.location.reload();
                                            //刷新列表
                                            parent.layui.table.reload("table-list")
                                            var index = parent.layer.getFrameIndex(window.name);
                                            parent.layer.close(index);
                                        });
                                    } else if (data.state == 0) {
                                        parent.layer.open({
                                            title: '提示'
                                            , content:"添加失败 " + data.msg
                                            , shade: 0.4
                                            , icon: 5
                                        });
                                    } else {
                                        parent.layer.open({
                                            title: '提示'
                                            , content: "添加异常 " + data.msg
                                            , shade: 0.4
                                            , icon: 2
                                        });
                                    }
                                }
                            });
                        } else {//修改
                            $.ajax({
                                url: "/Article/UpdateArticle",
                                type: "POST",
                                data: { id: id, title: title, content: content, tags: tags, summary: summary },
                                success: function (data) {
                                    layer.close(index)
                                    if (data.state == 1) {
                                        parent.layer.msg("修改成功", { shade: 0.1, icon: 1 }, function () {
                                            //parent.location.reload();
                                            //刷新列表
                                            parent.layui.table.reload("table-list")
                                            var index = parent.layer.getFrameIndex(window.name);
                                            parent.layer.close(index);
                                        });
                                    } else if (data.state == 0) {
                                        parent.layer.open({
                                            title: '提示'
                                            , content: "修改失败 " + data.msg
                                            , shade: 0.4
                                            , icon: 5
                                        });
                                    } else {
                                        parent.layer.open({
                                            title: '提示'
                                            , content: "修改异常 " + data.msg
                                            , shade: 0.4
                                            , icon: 2
                                        });
                                    }
                                }
                            });
                        }
                        
                    });


                });
            }

            return {
                init() {//初始化
                    if (!inited) {
                        initData();
                        inited = true;
                    }
                },
                cancel() {//取消
                    var index = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(index);
                }
            };
        }();
        ArticleAddOrUpdate.init();
    </script>
}
